<?php
/***************************************************************************
 *   copyright : (C) 2009 Udrea Cristian
 *   site : http://code.google.com/p/testauction-php
 ***************************************************************************/

/***************************************************************************
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as 
 *   published by the Free Software Foundation; either version 2 of the
 *   License, or (at your option) any later version. Although none of the
 *   code may be sold. If you have been sold this script, get a refund.
 ***************************************************************************/
require_once("MDB2.php");

function connect() {
	$url = DB_PROTOCOL."://".DB_USER.":".DB_PASS."@".DB_SERVER."/".DB_NAME;
    $con = MDB2::factory($url);
    if(PEAR::isError($con)) {
        die('Error while connecting : '.$con->getMessage());
    }
    return $con;
}

function execute_query($sql, $value_array = Array()) {
	$con = connect();
    $results = array();
    if(sizeof($value_array) > 0) {
        $statement = $con->prepare($sql, null, MDB2_PREPARE_RESULT);
        $resultset = $statement->execute($value_array);
        $statement->free();
    }
    else {
        $resultset = $con->query($sql);
    }
    if(PEAR::isError($resultset)) {
        die('DB Error: '.$resultset->getMessage());
    }
    while($row = $resultset->fetchRow(MDB2_FETCHMODE_ASSOC)) {
		$results[] = $row;
    }
	$resultset->free();
	$con->disconnect();
    return $results;
}

function execute_update($sql, $value_array = Array()) {
	$con = connect();
    if(sizeof($value_array) > 0) {
        $statement = $con->prepare($sql, null, MDB2_PREPARE_RESULT);
        $resultset = $statement->execute($value_array);
		$statement->free();
    }
    else {
        $resultset = $con->query($sql);
	}
    if(PEAR::isError($resultset)) {
		$con->disconnect();
		return FALSE;
	}
	$resultset->free();
	$con->disconnect();
	return TRUE;
}

/**
* generateRandStr - Generates a string made up of randomized
* letters (lower and upper case) and digits, the length
* is a specified parameter.
*/
function generateRandStr($length) {
	$randstr = "";
	for($i=0; $i<$length; $i++){
		$randnum = mt_rand(0,61);
		if($randnum < 10){
			$randstr .= chr($randnum+48);
		}else if($randnum < 36){
			$randstr .= chr($randnum+55);
		}else{
			$randstr .= chr($randnum+61);
		}
	}
	return $randstr;
}

/**
* generateRandID - Generates a string made up of randomized
* letters (lower and upper case) and digits and returns
* the md5 hash of it to be used as a userid.
*/
function generateRandID() {
	return md5(generateRandStr(16));
}

/* generateUniqueActivationCode */
function generateUniqueActivationCode() {
	$activationkey = generateRandStr(32);
	$q = 'SELECT * FROM '.TBL_TEMP_USERS.' WHERE activationkey = ?';
	$result = execute_query($q, array($activationkey));
	while(count($result) != 0){
		$activationkey = md5(generateRandStr(32));
		$result = execute_query($q, array($activationkey));
	}
	return $activationkey;
}

/* generateUserKey */
function generateUserKey() {
	$userkey = md5(generateRandStr(32));
	$q = 'SELECT * FROM '.TBL_USERS.' WHERE userkey = ?';
	$result = execute_query($q, array($userkey));
	while(count($result) != 0){
		$userkey = md5(generateRandStr(32));
		$result = execute_query($q, array($userkey));
	}
	return $userkey;
}
?>
